using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Fieldbook.Entities;
using Fieldbook.Common;
namespace Fieldbook.DAL
{
	public static class RoleDAO
	{
		#region Insert
		private static DbCommand insertCommand = null;
		private static string insertSQL = "INSERT INTO dbo.Role (DESCRIPTION) VALUES (@DESCRIPTION) SET @ROLEID = SCOPE_IDENTITY()";
		private static DbCommand GetInsertCommand(SqlDatabase db)
		{
			if (insertCommand == null)
			{
				insertCommand = db.GetSqlStringCommand(insertSQL);
				db.AddOutParameter(insertCommand, "@ROLEID", DbType.Int32, 4);
				db.AddInParameter(insertCommand, "@DESCRIPTION", DbType.String);
			}
			return insertCommand;
		}
		private static void SetInsertValues(SqlDatabase db, DbCommand cmd, Role entity)
		{
			db.SetParameterValue(cmd, "@DESCRIPTION", entity.Description);
		}
		public static bool InsertRole(Role entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
			DbCommand cmd = GetInsertCommand(db);
			SetInsertValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			if (res)
			{
				entity.Roleid = (int?)db.GetParameterValue(cmd, "@ROLEID");
			}
			return res;
		}
		public static bool InsertRole(Role entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = false;
			DbCommand cmd = GetInsertCommand(db);
			SetInsertValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			if (res)
			{
				entity.Roleid = (int?)db.GetParameterValue(cmd, "@ROLEID");
			}
			return res;
		}
		public static bool InsertListRole(List<Role> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				DbCommand cmd = GetInsertCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Role entity = list[i];
					SetInsertValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
					if (res)
					{
						entity.Roleid = (int?)db.GetParameterValue(cmd, "@ROLEID");
					}
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool InsertListRole(List<Role> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			DbCommand cmd = GetInsertCommand(db);
			for(int i=0; i<list.Count; i++)
			{
				Role entity = list[i];
				SetInsertValues(db, cmd, entity);
				res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				if (res)
				{
					entity.Roleid = (int?)db.GetParameterValue(cmd, "@ROLEID");
				}
			}
			return res;
		}
		#endregion
		#region Update
		private static DbCommand updateCommand = null;
		private static string updateSQL = "UPDATE dbo.Role SET DESCRIPTION = @DESCRIPTION WHERE ROLEID = @ROLEID";
		private static DbCommand GetUpdateCommand(SqlDatabase db)
		{
			if (updateCommand == null)
			{
				updateCommand = db.GetSqlStringCommand(updateSQL);
				db.AddInParameter(updateCommand, "@ROLEID", DbType.Int32);
				db.AddInParameter(updateCommand, "@DESCRIPTION", DbType.String);
			}
			return updateCommand;
		}
		private static void SetUpdateValues(SqlDatabase db, DbCommand cmd, Role entity)
		{
			db.SetParameterValue(cmd, "@ROLEID", entity.Roleid);
			db.SetParameterValue(cmd, "@DESCRIPTION", entity.Description);
		}
		public static bool UpdateRole(Role entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
			DbCommand cmd = GetUpdateCommand(db);
			SetUpdateValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool UpdateRole(Role entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = false;
			DbCommand cmd = GetUpdateCommand(db);
			SetUpdateValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			return res;
		}
		public static bool UpdateListRole(List<Role> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				DbCommand cmd = GetUpdateCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Role entity = list[i];
					SetUpdateValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool UpdateListRole(List<Role> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			DbCommand cmd = GetUpdateCommand(db);
			for(int i=0; i<list.Count; i++)
			{
				Role entity = list[i];
				SetUpdateValues(db, cmd, entity);
				res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
			}
			return res;
		}
		
		#endregion
		#region Save
		public static bool SaveRole(Role entity)
		{
			bool res = true;
			if (entity.Roleid == null )
			{
				res = InsertRole(entity);
			}
			else
			{
				res = UpdateRole(entity);
			}
			return res;
		}
		public static bool SaveRole(Role entity, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			if (entity.Roleid == null )
			{
				res = InsertRole(entity, transactionObj, db);
			}
			else
			{
				res = UpdateRole(entity, transactionObj, db);
			}
			return res;
		}
		public static bool SaveListRole(List<Role> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Role entity = list[i];
					res = res & SaveRole(entity, transactionObj, db);
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		public static bool SaveListRole(List<Role> list, DbTransaction transactionObj, SqlDatabase db)
		{
			bool res = true;
			for(int i=0; i<list.Count; i++)
			{
				Role entity = list[i];
				res = res & SaveRole(entity, transactionObj, db);
			}
			return res;
		}
		#endregion
		#region Delete
		private static DbCommand deleteCommand = null;
		private static string deleteSQL = "DELETE FROM dbo.Role WHERE ROLEID = @ROLEID";
		private static DbCommand GetDeleteCommand(SqlDatabase db)
		{
			if (deleteCommand == null)
			{
				deleteCommand = db.GetSqlStringCommand(deleteSQL);
				db.AddInParameter(deleteCommand, "@ROLEID", DbType.Int32);
			}
			return deleteCommand;
		}
		private static void SetDeleteValues(SqlDatabase db, DbCommand cmd, Role entity)
		{
			db.SetParameterValue(cmd, "@ROLEID", entity.Roleid);
		}
		public static bool DeleteRole(Role entity)
		{
			bool res = false;
			SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
			DbCommand cmd = GetDeleteCommand(db);
			SetDeleteValues(db, cmd, entity);
			res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
			return res;
		}
		public static bool DeleteListRole(List<Role> list)
		{
			bool res = true;
			DbTransaction transactionObj = null;
			DbConnection conn = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				DbCommand cmd = GetDeleteCommand(db);
				conn = db.CreateConnection();
				conn.Open();
				transactionObj = conn.BeginTransaction();
				for(int i=0; i<list.Count; i++)
				{
					Role entity = list[i];
					SetDeleteValues(db, cmd, entity);
					res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
				}
				transactionObj.Commit();
				transactionObj.Dispose();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception ex)
			{
				res = false;
				if (transactionObj != null)
				{
					transactionObj.Rollback();
					transactionObj.Dispose();
				}
				if (conn != null & conn.State != ConnectionState.Closed)
				{
					conn.Close();
					conn.Dispose();
				}
				throw ex;
			}
			return res;
		}
		
		#endregion
		#region Select
		private static string selectSQL = "SELECT ROLEID,DESCRIPTION FROM dbo.Role {0} {1}";
		
		public static List<Role> LoadRoles(string where, string orderby)
		{
			List<Role> res = new List<Role>();
			string sql = String.Format(selectSQL, where, orderby);
			IDataReader reader = null;
			try
			{
				SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
				reader = db.ExecuteReader(CommandType.Text, sql);
				if (reader != null)
				{
					while (reader.Read())
					{
						Role entity = new Role();
						entity.Roleid = reader.IsDBNull(0) ? null : (int?)reader.GetInt32(0);
						entity.Description = reader.IsDBNull(1) ? null : (string)reader.GetString(1);
						res.Add(entity);
					}
				}
				reader.Close();
			}
			catch (Exception ex)
			{
				if (reader != null && !reader.IsClosed)
				{
					reader.Close();
				}
				res = null;
				throw ex;
			}
			return res;
		}
		#endregion
	}
}
